<!-- Student_studyCourseDetail.html -->
<!-- studyCourseDetail.html -->
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
		<meta name="format-detection" content="telephone=no, email=no, date=no, address=no">
		<title>课程详情</title>
		<link rel="stylesheet" type="text/css" href="../../css/api.css" />
		<link rel="stylesheet" type="text/css" href="../../css/weui.min.css" />
		<link rel="stylesheet" href="../../css/CFNetPP_Base.css">
		<link rel="stylesheet" href="../../css/base.css" />
		<style type="text/css">
			/*公共css*/
			body {
				background-color: #fff;
			}
			.test_cell {
				width: 100px;
			}
			.test_cell_title {
				font-size: 14px;
				height: 44px;
			}
			#imageContainer {
				float: left;
				margin: 0;
				width: 40%;
				height: 120px;
				display: -webkit-flex;
				display: flex;
				align-items: center;
				justify-content: center;
				background-color: white;
				overflow: hidden;
			}
			#titleContainer {
				float: right;
				width: 60%;
			}
			ul {
				margin: 14px;
			}
			li {
				font-size: 12px;
				margin-top: 10px;
			}
			.courseInfoTitle {
				color: white;
				font-size: 14px;
				font-weight: normal;
			}
			/*#studyBtn{
			 width: 60px;
			 height: 30px;
			 background-color: #188eee;
			 border:1px solid #188eee;
			 border-radius: 4px;
			 color: white;
			 font-size: 14px;
			 }*/
			.tabbar {
				height: 50px;
				line-height: 50px;
				text-align: center;
				border-bottom: 1px solid #e0e0e0;
				background: #fff;
			}
			.tabbar .col {
				font-size: 14px;
			}
			.row {
				display: -webkit-box;
				display: -webkit-flex;
				display: flex;
			}
			.col {
				-webkit-box-flex: 1;
				-webkit-flex: 1;
				flex: 1;
				position: relative;
			}
			.indicator-super-div{
				width: 100%;
				height: 2px;
				padding: 0 20%;
				overflow: hidden;
			}
			.indicator {
				height: 2px;
				width: 62%;
				background: #18b4ed;
				position: absolute;
				bottom: 0;
			}
			.weui_tabbar {
				width: 100%;
				position: fixed;
				left: 0;
				bottom: 0;
			}
			.title_bulk {
				min-width: 60px;
				height: 44px;
			}
			.back_btn {
				color: white;
				font-size: 16px;
			}
			:focus {
				outline: 0;
			}
			* {
				-webkit-tap-highlight-color: rgba(255,255,255,0);
			}
			input, textarea {
				-webkit-appearance: none;
				border-radius: 0;
			}
			#nav_title {
				font-size: 16px;
				text-overflow: ellipsis;
				overflow: hidden;
				white-space: nowrap;
				word-break: keep-all;
				max-width: 200px;
			}
			#bottom_title {
				float: left;
				height: 52px;
				line-height: 52px;
			}
			#bottom_checkbox {
				float: left;
				padding: 14px;
			}
			#my_study_class {
				width: 120px;
				height: 34px;
				line-height: 34px;
				margin-top: 8px;
				margin-right: 14px;
				float: right;
			}
			#footer_bar {
				position: fixed;
				left: 0;
				bottom: 0;
				height: 52px;
				width: 100%;
				background-color: #f2f2f2;
			}
			#unselected_bottom {
				border-top: 1px solid #ddd;
			}
			#downloadBtn {
				display: block;
				width: 100%;
				height: 30px;
			}
			.whitebg {
				background-color: transparent;
				border-radius: 5px;
				display: block;
				overflow: hidden;
				margin-top: 10px;
				width: 84px;
				height: 30px;
			}
			#download_div {
				visibility: hidden;
			}
			.downloadNotEnbaled {
				background-color: #dfdfdf;
				color: #454545;
				/*border:1px solid #dfdfdf;*/
				border-radius: 5px;
				width: 100%;
				height: 30px;
				text-align: center;
				line-height: 30px;
				font-size: 14px;
				display: none;
			}
			#courseInfo {
				/*background-color: #48baff; */
				overflow: hidden;
				height: 158px;
				/*max-height: 200px;*/
				background-image: url(../../image/img_failed.png);
				background-size: 100% 100%;
			}
			.course_info_right_div {
				width: 40%;
				height: 158px;
				/*max-height: 200px;*/
				/*margin: 0;*/
				float: right;
				background-color: rgba(0, 0, 0, 0.5);
				position: relative;
				top: 0;
				right: 0;
				overflow: auto;
			}
			.cancel_download_btn {
				background-image: url(../../image/c.png);
				background-size: 84px 30px;
			}
			.download_btn {
				background-image: url(../../image/a.png);
				background-size: 84px 30px;
			}
			.download_finished_btn {
				background-image: url(../../image/d.png);
				background-size: 84px 30px;
				height: 30px;
				width: 84px;
			}
			.course_exam_div {
				background-color: #ff6601;
				overflow: hidden;
				height: 100%;
				text-align: center;
				color: white;
				font-size: 20px;
				height: 52px;
				line-height: 52px;
			}
		</style>
		<link rel="stylesheet" type="text/css" href="../../css/study.css" />
	</head>
	<body>
		<div class="base_style_color " id='BarAppearance' style="height: 22px;width: 100%"></div>
		<div id="header" class="base_navigation_bar base_style_color base_navigation_item_bar">
			<a class="base_left_item" href="#" onclick="backClick()"> <img class="base_back_icon" src="../../image/icon_jiantou.png" alt=""> </a>
			<span id="nav_title"></span>
			<a class="base_right_item base_hide_item"> </a>
		</div>
		<div id="main" class="base_content_div">
			<div id="courseInfo">
				<div class="course_info_right_div">
					<ul>
						<li>
							<label id="course_title" class="courseInfoTitle"></label>
						</li>
						<li>
							<label class="courseInfoTitle">主讲人：<label id="course_teacher"></label></label>
						</li>
						<li>
							<label class="courseInfoTitle">课时：<label id="course_time"></label>分钟</label>
						</li>
						<li>
							<span id="download_div" class="whitebg"> <button id="downloadBtn" href="javascript:;" class="download_btn" onclick="onDownloadClick();"></button> <label id="downloadComplete" class="download_finished_btn"></label> </span>
						</li>
					</ul>
				</div>
			</div>
			<div id="selectBar" class="row hightitem tabbar" >
				<div class="col base_dark_grey base_content_font" id="col1" tapmode onclick="tabswitch(1)">
					章节
					<div class="indicator-super-div">
						<div class="indicator" id="indicator1"></div>
					</div>
				</div>
				<div class="col base_dark_grey base_content_font"  id="col2" tapmode onclick="tabswitch(2)">
					答疑
					<div class="indicator-super-div">
						<div class="indicator" id="indicator2"></div>
					</div>
				</div>
				<div class="col base_dark_grey base_content_font" id="col3" tapmode onclick="tabswitch(3)">
					笔记
					<div class="indicator-super-div">
						<div class="indicator" id="indicator3"></div>
					</div>
				</div>
				<div class="col base_dark_grey base_content_font" id="col3" tapmode onclick="tabswitch(4)">
					资料
					<div class="indicator-super-div">
						<div class="indicator" id="indicator4"></div>
					</div>
				</div>
			</div>
		</div>
		<div id="bottom_bar" class="course_select_footer">
			<div id="unadded_status" class="course_select_unadded_div" onclick="addClick();">
				<img src="../../image/icon_add.png" class="course_select_add_img" alt="" />
				<label class="course_select_title">添加课程</label>
			</div>
			<div id="added_status" class="course_select_added_div" hidden="hidden">
				<div class="course_select_delete_div">
					<div class="course_select_delete_img_div">
						<img class="course_select_delete_img" src="../../image/icon_delete.png" alt="">
					</div>
					<div class="course_select_delete_text_div" onclick="onDeleteClick();">
						<label class="course_select_delete_text">移除</label>
					</div>
				</div>
				<div id="course_bottom_btn" class="course_select_added_title_div" onclick="onStudyClick();">
					<label id="course_bottom_btn_title" class="course_select_added_title">进入学习</label>
				</div>
			</div>
		</div>
		<div class="noSign noDataDiv" hidden="hidden" id="noData">
			<img src="../../image/icon_img.png" width="60%">
			<p class="font16 top_15 base_dark_grey" >
				暂无数据
			</p>
		</div>
		<div class="loadFailure noInternetDiv" hidden="hidden" id="noNet">
			<img src="../../image/icon_img01.png" width="60%">
			<p class="" >
				数据加载失败
			</p>
			<p>
				请检查您的网络
			</p>
			<button type="button" class="mui-btn mui-btn-outlined" onclick="loadData()">
				重新加载
			</button>
		</div>
	</body>
	<script src="../../script/jquery.min.js"></script>
	<script type="text/javascript" src="../../script/api.js"></script>
	<script type="text/javascript" src="../../script/requestTool.js"></script>
	<script type="text/javascript" src="../../script/databaseOperation.js"></script>
	<script type="text/javascript" src="../../script/study_courseTimeRecord.js"></script>
	<script type="text/javascript">
		var openSubFrame;
		var openSubFrameGroup;
		var tabswitch;
		var setupGroupIndex;
		var openCourseDetailGroup;
		var closeCourseDetailGroup;
		var onBackClick;
		var onStudyClick;
		var selectedType = 0;
		var addClick;
		var onDeleteClick;
		var onDownloadClick;
		var course_param;
		var selected;
		var courseChecks = 0;
		var totalCourseTime;
		var courseExamid = new String;
		var examSid;
		var crid;
		var cstatusType;
		// var frameType = 2;
		var uid;
		var jid;
		var isSendUploadNotifi = false;
		DBReady = function() {
			prepareData();
		
			course_param = api.pageParam;
			crid = course_param.crid;
			//请求浏览接口(后台记录浏览次数)
			requestBrowse();
			//加载数据
			loadData();
			var navtitle = course_param.title;
			var selected = course_param.selected;
			if (selected == 1) {//已选课程
				$('#unadded_status').hide();
				$('#added_status').show();
			}
			cstatusType = course_param.cstatus;
			var cstatus = course_param.cstatus;
			if (cstatus == 0) {
				$('.course_select_delete_div').hide();
				$('#course_bottom_btn').css({
					'width' : '100%'
				});
			} else {
				$('.course_select_delete_div').show();
			}
			// $api.attr($api.byId('course_img'), 'src', course_param.imgurl);
			// var courseInfoBackgroundImg = "background-image: url(" + course_param.imgurl + ");"
			// $api.css($api.byId('courseInfo'), courseInfoBackgroundImg);
			$api.text($api.byId('course_title'), navtitle);
			$api.text($api.byId('nav_title'), navtitle);
			var name = "courseDetil";
			// var y = $('#header').height() + $('#courseInfo').innerHeight() + $('#selectBar').outerHeight();
			openSubFrameGroup = function() {
				var y = $api.byId('BarAppearance').offsetHeight + $api.byId('header').offsetHeight + $api.byId('courseInfo').offsetHeight + $api.byId('selectBar').offsetHeight;
				var h = api.winHeight - y - 52;
				if (selectedType == 1) {
					h += 52;
					$api.attr($api.byId('footer_bar'), 'hidden', 'hidden');
				}
				var questionParams = api.pageParam;
				questionParams['coursename'] = api.pageParam.title;
				api.openFrameGroup({
					name : name,
					background : '#fff',
					scrollEnabled : false,
					rect : {
						x : 0,
						y : y,
						w : 'auto',
						h : h
					},
					index : 0,
					preload : 0,
					frames : [
					// {
					// 	name : 'studyCourseDescription',
					// 	url : 'studyCourseDescription.html',
					// 	bgColor : '#fff',
					// 	pageParam : {
					// 		description : navtitle,
					// 		cid : course_param.courseid,
					// 		selected : selected,
					// 		source : course_param.source,
					// 		part : course_param.part
					// 	},
					// 	bounces : false
					// }, 
					{
						name : 'studyCourseChapter',
						url : 'studyCourseChapter.html',
						bgColor : '#fff',
						pageParam : {
							courseid : course_param.courseid,
							selected : selected,
							courseChecks : courseChecks,
							cstatus : course_param.cstatus,
							imgurl : course_param.imgurl,
							source : course_param.source,
							crid : crid,
							examid : course_param.examid,
							pid : course_param.planid
						},
						bounces : false
					},{
				        name: 'Teacher_StudyCourseQA', 
				        url: '../Teacher_Teach/Teacher_StudyCourseQA.html',
				        pageParam: questionParams
				    },{
				        name: 'Teacher_StudyCourseNote', 
				        url: '../Teacher_Teach/Teacher_StudyCourseNote.html',
				        pageParam: questionParams
				    },{
				        name: 'Teacher_StudyCourseMaterials', 
				        url: '../Teacher_Teach/Teacher_StudyCourseMaterials.html',
				        pageParam: api.pageParam
				    }]
				}, function(ret, err) {
					// if( ret ){
					// }else{
					// }
				});
			}
			openCourseDetailGroup = function() {
				api.setFrameGroupAttr({
					name : name,
					hidden : false
				});
			}
			closeCourseDetailGroup = function() {
				api.setFrameGroupAttr({
					name : name,
					hidden : true
				});
			}
			//处理bug，sb原理**************
			//// loadCommentListData
			api.addEventListener({
				name : 'loadCommentListData'
			}, function(ret, err) {
			});
			// removeComment
			api.addEventListener({
				name : 'removeComment'
			}, function(ret, err) {
			});
			api.addEventListener({
				name : 'selectedCourseNotifi'
			}, function(ret, err) {
			});
			api.addEventListener({
				name : 'startDownloadCourse'
			}, function(ret, err) {
			});
			api.addEventListener({
				name : 'startStudy'
			}, function(ret, err) {
			});
			api.addEventListener({
				name : 'cancelDownloadCourse'
			}, function(ret, err) {
			});
			api.addEventListener({
				name : 'cancelledChapter'
			}, function(ret, err) {
			});
			api.addEventListener({
				name : 'downlaodNotifi'
			}, function(ret, err) {
			});
			api.addEventListener({
				name : 'courseStudiedFinishNotifi'
			}, function(ret, err) {
			});
			api.addEventListener({
				name : 'eventType'
			}, function(ret, err) {
				api.alert({
					msg : 'eventType'
				});
			});
			api.addEventListener({
				name : 'downloadingMaterialNotifi'
			}, function(ret, err) {
			});
			api.addEventListener({
				name : 'changeCrid'
			}, function(ret, err) {
			});
			api.addEventListener({
				name : 'closewindow'
			}, function(ret, err) {
			});
			//****************************
			//考试结束通过
			api.addEventListener({
				name : 'studyPassedExamNotifi'
			}, function(ret, err) {
				// alert('详情页:'+JSON.stringify(ret));
				if (ret.value.isthrough == 1) {
					setupExamBtnStatus(1);
				}
			});
			api.addEventListener({
				name : 'closeCourseDetailGroup'
			}, function(ret, err) {
				closeCourseDetailGroup();
			});
			api.addEventListener({
				name : 'openCourseDetailGroup'
			}, function(ret, err) {
				openCourseDetailGroup();
			});
			api.addEventListener({
				name : 'studyPassedExamNotifi'
			}, function(ret, err) {
				if (ret.value.cid == api.pageParam.examid) {
					api.sendEvent({
						name : 'finishStudy',//所有章节学完通知
					});
					setupExamBtnStatus(2);
				}
			});
			//监听取消下载完成
			api.addEventListener({
				name : 'cancelDownloadFinish'
			}, function(ret, err) {
				$('#downloadBtn').removeClass('cancel_download_btn');
				$('#downloadBtn').addClass('download_btn');
				// $('#downloadBtn').text('点击下载');
			});
			//监听全部章节学完监听
			api.addEventListener({
				name : 'allChapterStudiedNotifi'
			}, function(ret, err) {
				if (api.pageParam.examid == '0') {
					api.sendEvent({
						name : 'finishStudy',//所有章节学完通知
					});
				}
				allChapteretupExamBtnFromDB();
				// var updateRecordSql = "UPDATE t_study_course_record SET submitstatus=2, status=1,timenum=" + totalCourseTime + " WHERE vid=0 AND cid=" + course_param.courseid + " AND uid=" + uid + ";";
				// executeSQL(updateRecordSql, function(ret, err){
				// 	api.sendEvent({
				// 	    name: 'uploadCourseRecordNotifi'
				// 	});
				// });
			});
			api.addEventListener({
				name : 'viewwillappear'
			}, function(ret, err) {
				setupExamBtnFromDB();
				//上传未上传的学习记录
				// api.sendEvent({
				//        name: 'uploadCourseRecordNotifi'
				//    });
			});
			api.addEventListener({
				name : 'viewdisappear'
			}, function(ret, err) {
				// if (frameType == 2) {
				// 	tabswitch(2);
				// }
			});
			setupGroupIndex = function(type) {
				api.setFrameGroupIndex({
					name : name,
					index : type - 1,
					scroll : true
				});
			};
			tabswitch = function(index) {
				$('.indicator').attr('style','display:none;');
				$('.col').attr('style', 'color:#323232;');
				$('#col'+index).attr('style','color:#18b4ed;');
				$('#indicator'+index).attr('style','display:block;');
				setupGroupIndex(index);
				return;
				var col1 = $api.byId('col1');
				var col2 = $api.byId('col2');
				var indicator1 = $api.byId("indicator1");
				var indicator2 = $api.byId("indicator2");
				if (index == 1) {
					frameType = 1;
					col1.style.color = "#18b4ed";
					col2.style.color = "#323232";
					indicator1.style.display = "block";
					indicator2.style.display = "none";
				} else if (index == 2) {
					frameType = 2;
					col2.style.color = "#18b4ed";
					col1.style.color = "#323232";
					indicator1.style.display = "none";
					indicator2.style.display = "block";
				}
				setupGroupIndex(index);
			}
			tabswitch(1);
			var bottomBtnTitle = $('#course_bottom_btn_title');
			onStudyClick = function() {
				if (bottomBtnTitle.text() == '进入学习') {
					api.sendEvent({
						name : 'startStudy'
					});
					// api.openWin({useWKWebView:true,
					//     name: 'Student_examDetail_finish',
					//     url: 'Student_examDetail_finish.html',
					//     bounces: false,
					//     pageParam: param,
					//     slidBackEnabled:false
					// });
				} else if (bottomBtnTitle.text() == '开始考核') {
					var courseid = course_param.courseid;
					var pid = course_param.planid;
					var param = {
						examid : courseExamid,
						cid : courseid,
						pid : pid
					};
					var connectionType = api.connectionType + '';
					if (connectionType == 'none') {
						api.toast({
							msg : '网络异常，请检查网络'
						});
					} else {
						api.openWin({useWKWebView:true,
							name : 'MoccCourse_TestExam',
							url : 'MoccCourse_TestExam.html',
							bounces : false,
							pageParam : param,
							slidBackEnabled : false
						});
					}
				} else if (bottomBtnTitle.text() == '已通过考核') {
				}
			}
			addClick = function() {
				api.confirm({
					msg : '是否添加该课程',
					buttons : ['取消', '确定']
				}, function(ret, err) {
					if (ret) {
						if (ret.buttonIndex == 2) {//确定
							var resParams = {
								values : {
									jid : jid,
									// jid:'220',
									uid : uid,
									cid : course_param.courseid,
									source : course_param.source,
									title : course_param.title
								}
							};
							api.ajax({
								url : study_select_course,
								method : 'post',
								timeout : 10,
								dataType : 'json',
								returnAll : false,
								data : resParams
							}, function(ret, err) {
								if (ret) {
									if (ret.status == '1' || ret.status == 'true') {
										cstatusType = '1';
										api.toast({
											msg : '已成功添加该课程',
											duration : 2000,
											location : 'bottom'
										});
										api.sendEvent({
											name : 'changeCrid',
											extra : {
												crid : ret.crid
											}
										});
										api.sendEvent({
											name : 'refreshCourseList'
										});
										selected = 1;
										api.sendEvent({//发送添加课程通知
											name : 'selectedCourseNotifi',
											extra : {
												selected : selected
											}
										});
										$('#unadded_status').hide();
										$('#added_status').show();
										if (ret.crid) {
											if (ret.crid != null && ret.crid != '' && ret.crid != 'undefined') {
												openDB(function(ret, err) {
													if (ret.status) {
														var updateRecordSql = "UPDATE t_study_course_record SET crid=" + ret.crid + " WHERE vid=0 AND uid=" + uid + "  AND cid=" + course_param.courseid + ";";
														executeSQL(updateRecordSql, function(ret, err) {
															// if (ret.status) {
															// }else{
															// }
															if (isSendUploadNotifi == true) {//发送上传通知
																api.sendEvent({
																	name : 'uploadCourseRecordNotifi'
																});
															}
														});
													}
												});
											}
										}
									} else {
										api.toast({
											msg : "数据错误",
											duration : 2000,
											location : 'bottom'
										});
									}
								} else {
									api.toast({
										msg : '2网络异常,请检查网络',
										duration : 2000,
										location : 'bottom'
									});
								};
							});
						}
					} else {
					}
				});
			}
			onDeleteClick = function() {
				// if (cstatusType != 1) {
				// 	api.toast({
				// 	    msg: '此课程不可移除',
				// 	    duration: 3000,
				// 	    location: 'bottom'
				// 	});
				// 	return;
				// }
				api.confirm({
					msg : '是否移除该课程',
					buttons : ['取消', '确定']
				}, function(ret, err) {
					if (ret) {
						if (ret.buttonIndex == 2) {//确定
							var resParams = {
								values : {
									jid : jid,
									// jid:'220',
									uid : uid,
									cid : course_param.courseid,
									source : course_param.source
								}
							};
							api.ajax({
								url : study_delete_course,
								method : 'post',
								timeout : 10,
								dataType : 'json',
								returnAll : false,
								data : resParams
							}, function(ret, err) {
								if (ret) {
									if (ret.status == '1') {
										cstatusType = '0';
										api.toast({
											msg : '已移除该课程',
											duration : 2000,
											location : 'bottom'
										});
										api.sendEvent({
											name : 'refreshCourseList'
										});
										//  	api.sendEvent({
										//     name: 'cancelDownloadCourse',
										//     extra: {courseid: course_param.courseid}
										// });
										selected = 0;
										api.sendEvent({//发送添加课程通知
											name : 'selectedCourseNotifi',
											extra : {
												selected : selected
											}
										});
										$('#added_status').hide();
										$('#unadded_status').show();
										//删除该课程缓存
										// removeCourseCache(course_param.courseid);
									} else {
										api.toast({
											msg : '数据错误',
											duration : 2000,
											location : 'bottom'
										});
									}
								} else {
									api.toast({
										msg : '3网络异常,请检查网络',
										duration : 2000,
										location : 'bottom'
									});
								};
							});
						}
					} else {
					}
				});
			}
			onDownloadClick = function() {
				if ($('#downloadBtn').hasClass('download_finished_btn')) {
					return;
				}
				if ($('#downloadBtn').hasClass('cancel_download_btn')) {
					//	 		 $("#downlogout").show();
					api.confirm({
						msg : '是否取消下载该课程',
						buttons : ['取消', '确定']
					}, function(ret, err) {
						if (ret.buttonIndex == 2) {//确定
							api.sendEvent({
								name : 'cancelDownloadCourse',
								extra : {
									courseid : course_param.courseid
								}
							});
							$('#downloadBtn').removeClass('cancel_download_btn');
							$('#downloadBtn').addClass('download_btn');
							// $('#downloadBtn').text('点击下载');
						}
					});
				} else {
					if (selected != 1) {
						api.toast({
							msg : '您还未添加该课程',
							duration : 3000,
							location : 'bottom'
						});
						return;
					}
					api.confirm({
						msg : '是否下载该课程',
						buttons : ['取消', '确定']
					}, function(ret, err) {
						if (ret) {
							if (ret.buttonIndex == 2) {//确定
								var connectionType = api.connectionType + '';
								if (connectionType == 'none') {
									api.toast({
										msg : '网络异常，请检查网络',
										duration : 2000,
										location : 'bottom'
									});
								} else {
									api.sendEvent({
										name : 'startDownloadCourse',
										extra : {
											courseid : course_param.courseid
										}
									});
									$('#downloadBtn').addClass('cancel_download_btn');
									$('#downloadBtn').removeClass('download_btn');
								}
								// $('#downloadBtn').text('取消下载');
							}
						} else {
						}
					});
				}
			}
	
			//监听下载失败
			api.addEventListener({
				name : 'downlaodFailedNotifi'
			}, function(ret, err) {
				if (ret.value.cid == course_param.courseid) {
					$('#downloadBtn').removeClass('cancel_download_btn');
					$('#downloadBtn').addClass('download_btn');
					// $('#downloadBtn').text('点击下载');
				}
			});
			api.addEventListener({//全部下载完成
				name : 'filesDownloadFinishNotifi'
			}, function(ret, err) {
				var download_course_id = ret.value.courseid;
				var download_course_size = ret.value.coursesize;
				if (course_param.courseid == download_course_id) {
					downloadComplete();
					
				}
			});
			
		}
		function loadData() {
			requestionData();
		}

		function loadFiled() {
			openDB(function(ret, err) {
				if (ret.status) {
					var sql = "SELECT * FROM t_study_course_info WHERE courseid = '" + course_param.courseid + "';";
					var num = 0;
					selectSQL(sql, function(ret, err) {
						if (ret.status && ret.data != undefined && ret.data != '' && ret.data != null) {
							var couinfo = ret.data[0];
							setupPage(couinfo);
						} else {
						}
						
					});
					setupExamBtnFromDB();
				}
			});
		}

		function setupExamBtnFromDB() {
			var recordSql = "SELECT status FROM t_study_course_record WHERE vid=0 AND cid=" + course_param.courseid + " AND uid=" + uid + ";";
			selectSQL(recordSql, function(ret, err) {
				if (ret.status && ret.data != undefined && ret.data != '' && ret.data != null) {
					if (ret.data[0].status == 1) {
						var examSql = "SELECT examid,examstatus,sid FROM t_study_exam WHERE courseid=" + course_param.courseid + " AND uid=" + uid + ";";
						selectSQL(examSql, function(ret, err) {
							if (ret.status && ret.data != undefined && ret.data != '' && ret.data != null) {
								var examid = ret.data[0].examid;
								courseExamid = examid;
								var sid = ret.data[0].sid;
								examSid = sid;
								var examstatus = ret.data[0].examstatus;
								setupExamBtn(examid, examstatus);
							}
						});
					}
					
				}
			});
		}

		function allChapteretupExamBtnFromDB() {
			var examSql = "SELECT examid,examstatus,sid FROM t_study_exam WHERE courseid=" + course_param.courseid + " AND uid=" + uid + ";";
			selectSQL(examSql, function(ret, err) {
				if (ret.status && ret.data != undefined && ret.data != '' && ret.data != null) {
					var examid = ret.data[0].examid;
					courseExamid = examid;
					var sid = ret.data[0].sid;
					examSid = sid;
					var examstatus = ret.data[0].examstatus;
					setupExamBtn(examid, examstatus);
				}
			});
		}

		function requestionData() {
			var urlString = study_course_detail;
			var params = {
				values : {
					uid : uid,
					cid : course_param.courseid,
					jid : jid,
					pid : course_param.planid,

				}
			};
			// alert('url:'+study_course_detail+'\nret:'+JSON.stringify(params));
			api.ajax({
				url : urlString,
				method : 'post',
				timeout : 30,
				dataType : 'json',
				returnAll : false,
				data : params
			}, function(ret, err) {
				// alert('ret:'+JSON.stringify(ret));
				if (ret) {
					if (ret.code == 200) {
						var couinfo = ret.data;
						setupPage(couinfo);
						var coursename = couinfo.coursename;
						var courseinfo = couinfo.courseinfo;
						var courseid = course_param.courseid;
						var coverimages = couinfo.coverimages
						var patt1 = new RegExp("http://");
						if (!patt1.test(coverimages)) {
							coverimages = 'http://cfnet.org.cn/data/uploads/' + coverimages;
						}
						var speaker = couinfo.speaker;
						var alltime = couinfo.alltime;
						var checks = couinfo.checks;
						var udapp = couinfo.udapp;
						var sqlParams = {
							coursename : coursename,
							courseinfo : courseinfo,
							courseid : courseid,
							coverimages : coverimages,
							speaker : speaker,
							alltime : alltime,
							checks : checks,
							udapp : udapp
						};
						var cid = courseid;
						var vid = '0';
						var uniquekey = cid + vid + uid;
						var status = couinfo.status;
						var timenum = couinfo.timenum;
						if (timenum == null) {
							timenum = 0;
						}
						var submitstatus = 0;
						// if (parseInt(timenum) >= parseInt(alltime)) {
						// 	timenum = alltime;
						// 	if (couinfo.examid == '') {
						// 		status = 1;
						// 		submitstatus = 1;
						// 		if (couinfo.status == 0) {
						// 			submitstatus = 2;
						// 			isSendUploadNotifi = true;
						// 		}
						// 	}else{
						// 		if (couinfo.examstatus == 1) {
						// 			status = 1;
						// 			submitstatus = 1;
						// 			if (couinfo.status == 0) {
						// 				submitstatus = 2;
						// 				isSendUploadNotifi = true;
						// 			}
						// 		}
						// 	}
						// }
						var chapterRecordParam = {
							uid : uid,
							jid : jid,
							pid : course_param.planid,
							vid : vid,
							cid : cid,
							crid : crid,
							types : '0', //课程
							timenum : timenum,
							status : status,
							origtime : alltime,
							submitstatus : submitstatus,
							uniquekey : uniquekey
						}
						//课程考试数据
						var examstatus = couinfo.examstatus;
						var examid = couinfo.examid;
						// if (parseInt(timenum) >= parseInt(alltime)) {
						// 	setupExamBtn(examid, examstatus);
						// }
						if (status == 1) {
							setupExamBtn(examid, examstatus);
						}
						courseExamid = examid;
						//设置课程考核id
						var sid = couinfo.sid;
						examSid = sid;
						//设置sid
						var pid = api.pageParam.planid;
						var examUniquekey = cid + uid;
						var examParam = {
							examid : examid,
							examstatus : examstatus,
							sid : sid,
							pid : pid,
							uid : uid,
							courseid : cid,
							uniquekey : examUniquekey
						};
						openDB(function(ret, err) {
							if (ret.status) {
								insertData('t_study_course_info', sqlParams, function(ret, err) {
									if (ret.status) {
									} else {
										var updateSql = "UPDATE t_study_course_info SET courseinfo='" + sqlParams.courseinfo + "' ,udapp='" + udapp + "', alltime='" + alltime + "' ,checks='" + checks + "' WHERE courseid='" + courseid + "';";
										executeSQL(updateSql, function(ret, err) {
											if (ret.status) {
											} else {
											}
										});
									}
								});
								insertData('t_study_course_record', chapterRecordParam, function(ret, err) {
									if (ret.status) {
										if (isSendUploadNotifi == true) {//发送上传通知
											api.sendEvent({
												name : 'uploadCourseRecordNotifi'
											});
										}
									} else {
										var updateRecordSql = "UPDATE t_study_course_record SET timenum='" + chapterRecordParam.timenum + "' ,submitstatus=" + submitstatus + ", origtime=" + alltime + ", crid=" + crid + ",status=" + chapterRecordParam.status + " WHERE vid=0 AND uid=" + uid + " AND cid=" + chapterRecordParam.cid + ";";
										executeSQL(updateRecordSql, function(ret, err) {
											// if (ret.status) {
											// }else{
											// }
											if (isSendUploadNotifi == true) {//发送上传通知
												api.sendEvent({
													name : 'uploadCourseRecordNotifi'
												});
											}
										});
									}
								});
								insertData('t_study_exam', examParam, function(ret, err) {
									if (ret.status) {
									} else {
										var updateExamSql = "UPDATE t_study_exam SET examid=" + examParam.examid + ", sid=" + sid + ", examstatus=" + examParam.examstatus + " WHERE courseid=" + examParam.courseid + " AND uid=" + examParam.uid + ";";
										executeSQL(updateExamSql, function(ret, err) {
										});
									}
								});
							}
						});
					} else {
						api.toast({
							msg : "暂无数据",
							duration : 2000,
							location : 'bottom'
						});
					}
				} else {
					api.toast({
						msg : '网络异常,请检查网络',
						duration : 2000,
						location : 'bottom'
					});
					$('#noData').show();
					loadFiled();
				};
			});
		}

		function setupExamBtnStatus(examstatus) {
			//暂时屏蔽
			// if (examstatus == 1) {
			// 	$('#course_bottom_btn_title').text('已通过考核');
			// } else {
			// 	$('#course_bottom_btn_title').text('开始考核');
			// }
		}

		function setupExamBtn(examid, examstatus) {
			//暂时屏蔽
			// if (examid != null && examid != undefined && examid != 0 && examid != '') {
			// 	$('#course_bottom_btn').removeClass('course_select_added_title_div');
			// 	$('#course_bottom_btn').addClass('course_exam_div');
			// 	setupExamBtnStatus(examstatus);
			// }
		}

		function requestBrowse() {
			api.ajax({
				url : study_course_browse,
				method : 'post',
				timeout : 10,
				dataType : 'json',
				returnAll : false,
				data : {
					values : {
						jid : jid,
						uid : uid,
						cid : course_param.courseid,
						source : course_param.source,
						part : course_param.part
					}
				}
			}, function(ret, err) {
				//			alert(JSON.stringify(ret))
				if (ret.status == 1) {
				} else {
//					api.toast({
//						msg : '暂无数据',
//						duration : 2000,
//						location : 'bottom'
//					});
				}
			});
		}

		function setupPage(couinfo) {
			var coverimages = couinfo.coverimages
			var patt1 = new RegExp("http://");
			if (!patt1.test(coverimages)) {
				coverimages = 'http://cfnet.org.cn/data/uploads/' + coverimages;
			}
			var courseInfoBackgroundImg = "background-image: url(" + coverimages + ");"
			$api.css($api.byId('courseInfo'), courseInfoBackgroundImg);
			courseChecks = couinfo.checks;
			openSubFrameGroup();
			totalCourseTime = couinfo.alltime;
			//设置总时间
			$('#course_teacher').text(couinfo.speaker);
			var alltimeStr = couinfo.alltime;
			if (alltimeStr == '' || alltimeStr == undefined) {
				alltimeStr = 0;
			}
			var alltimenum = parseInt(alltimeStr);
			$('#course_time').text(alltimenum);
			if (couinfo.udapp == 1) {
				$('#download_div').attr('style', 'visibility: visible;');
			}
			// api.sendEvent({
			//     name: 'courseDesNotifi',
			//     extra: {
			//     	courseinfo : couinfo.courseinfo
			//     }
			// });
			var courseSql = "SELECT iscache FROM t_study_course_info WHERE courseid = '" + course_param.courseid + "';";
			selectSQL(courseSql, function(ret, err) {
				if (ret.status && ret.data != null && ret.data != '' && ret.data != undefined) {
					var cacheStatus = ret.data[0].iscache;
					if (cacheStatus == 1) {
						downloadComplete();
					} else if (cacheStatus == 2) {//正在下载
						$('#downloadBtn').addClass('cancel_download_btn');
						$('#downloadBtn').removeClass('download_btn');
						// $('#downloadBtn').text('取消下载');
					}
				}
				// num++;
				// if (num == 2) {
				// 	closeDB(function(ret, err){});
				// }
			});
		}

		function downloadComplete() {
			$('#downloadBtn').hide();
			$('#downloadComplete').show();
		}

		function removeCourseCache(courseid) {
			var fs = api.require('fs');
			openDB(function(ret, err) {
				if (ret.status) {
					var sql = "SELECT cachepath FROM t_study_course_info WHERE courseid=" + courseid;
					selectSQL(sql, function(ret, err) {
						if (ret.status) {
							var filepath = ret.data[0].cachepath;
							fs.rmdir({
								path : filepath
							}, function(ret, err) {
								if (ret.status) {
									//更新t_study_course中iscache为0
									var sql = "UPDATE t_study_course_info SET iscache = '0' where courseid=" + courseid + ";";
									executeSQL(sql, function(ret, err) {
										//删除章节缓存地址
										var updateChapterSql = "UPDATE t_study_chapter SET localpath = null WHERE cid=" + courseid + ";";
										executeSQL(updateChapterSql, function(ret, err) {
											$('#downloadBtn').addClass('download_btn');
										});
									});
								} else {
								}
							});
						}
					});
				}
			});
		}

		function backClick() {
			api.sendEvent({
				name : 'closewindow'
			});
			api.closeWin({
				name : 'studyCourseDetail'
			});
		}

		function prepareData() {
			uid = $api.getStorage('cfnetppuid');
			jid = $api.getStorage('cfnetppjxid');
		}
	</script>
</html>